package de.iritgo.skillfull.component.visitor;

import java.awt.Point;
import java.awt.Rectangle;

import de.iritgo.skillfull.component.CollisionComponent;
import de.iritgo.skillfull.component.motion.MoveFieldComponent;
import de.iritgo.skillfull.component.motion.MoveRotationComponent;
import de.iritgo.skillfull.component.motion.PositionComponent;
import de.iritgo.skillfull.component.visual.AnimationRenderComponent;
import de.iritgo.skillfull.component.visual.ImageRenderComponent;
import de.iritgo.skillfull.component.visual.SimpleTextComponent;
import de.iritgo.skillfull.component.visual.SpriteSheetRenderComponent;
import de.iritgo.skillfull.component.visual.VirusComponent;
import de.iritgo.skillfull.entity.Entity;

/**
 * Control the move field component and configure the other components
 * 
 * @author held
 *
 */
public class MoveFieldVisitor extends BaseVisitor 
{
	private Point dst;
	private boolean done;
	private boolean setDestToMovieFieldCmp;

	int x,y = -1;
	private int special;
	private float direction;
	private Rectangle validArea;
	
	public MoveFieldVisitor (Point destination)
	{
		this.dst = destination;
		this.done = false;
		this.setDestToMovieFieldCmp = true;
	}
	
	public MoveFieldVisitor(int special) 
	{
		this.special = special;
	}

	public MoveFieldVisitor (float direction, Rectangle validArea) 
	{
		this.direction = direction;
		this.validArea = validArea;
		this.done = false;
		this.setDestToMovieFieldCmp = true;
	}

	public boolean isDone ()
	{
		return done;
	}
	
	public void visit(MoveRotationComponent moveRotationComponent) 
	{
		moveRotationComponent.setX (x);
		moveRotationComponent.setY (y);
	}
	
	public void visit (CollisionComponent collisionComponent) 
	{
		collisionComponent.setX (x);
		collisionComponent.setY (y);
		collisionComponent.moved ();
	}

	public void visit (SimpleTextComponent simpleTextComponent) 
	{
		simpleTextComponent.setX (x);
		simpleTextComponent.setY (y);
	}

	public void visit(VirusComponent virusComponent) 
	{
		virusComponent.setX (x);
		virusComponent.setY (y);
	}

	public void visit (MoveFieldComponent moveFieldComponent) 
	{
		if (special == 1)
		{
			moveFieldComponent.stop ();
			x = (int) moveFieldComponent.getX ();
			y = (int) moveFieldComponent.getY ();
			moveFieldComponent.setActive (false);
			done = true;
			return;
		}
		
		if (setDestToMovieFieldCmp)
		{
			moveFieldComponent.setActive (true);
			if (validArea == null)
			{
				moveFieldComponent.moveTo (dst);
			}
			else
			{
				moveFieldComponent.moveTo(direction, validArea);
			}
			setDestToMovieFieldCmp = false;
		}
		x = (int) moveFieldComponent.getX ();
		y = (int) moveFieldComponent.getY ();
		
		entity.setPosition (x, y);
		
		if (moveFieldComponent.dstReached ())
		{
			done = true;
		}
	}
	
	public void visit (PositionComponent positionComponent)	
	{	
		if (positionComponent.isRelative ())
		{
			x = x + (int) positionComponent.getX ();
			y = y + (int) positionComponent.getY ();
		}
		else	
		{
			x = (int) positionComponent.getX ();
			y = (int) positionComponent.getY ();
		}
	}

	public void visit (ImageRenderComponent renderComponent)
	{
		renderComponent.setX (x);
		renderComponent.setY (y);
	}

	public void visit (AnimationRenderComponent animationRenderComponent)
	{
		animationRenderComponent.setX (x);
		animationRenderComponent.setY (y);
	}

	public void visit(SpriteSheetRenderComponent spriteSheetRenderComponent) 
	{ 
		spriteSheetRenderComponent.setX (x);
		spriteSheetRenderComponent.setY (y);
	}

}
